package views;

import model.Problem;
import model.Solution;
import model.SolverEnvironment;
import processing.core.PApplet;

public class VisualizeLoadCostApplet extends PApplet {
	final static int machine_width = 10;
	final static int resource_height = 20;

	SolverEnvironment env;
	Problem problem;
	public Solution currentSolution;
	long max_cost_contribution = Long.MIN_VALUE;

	public VisualizeLoadCostApplet(SolverEnvironment env) {
		this.env = env;
		this.problem = env.getProblem();
		this.currentSolution = env.getCurrentSolution();
	}

	public void setup() {
		int width = problem.M * machine_width, height = problem.RE
				* resource_height;
		size(width, height);
		background(255, 255, 255);
	}

	public void draw() {
		if (currentSolution.load_cost_max_contribution_value > 0) {
			if (currentSolution.load_cost_max_contribution_value > max_cost_contribution)
				max_cost_contribution = currentSolution.load_cost_max_contribution_value;
			for (int i = 0; i < problem.M; i++) {
				for (int j = 0; j < problem.RE; j++) {
					if (currentSolution.load_cost_contribution_unzipped[i][j] > 0) {
						long n = currentSolution.load_cost_contribution_unzipped[i][j];
						int r = (int) (255 * n / (double) max_cost_contribution);
						int g = (int) ((255 * (max_cost_contribution - n)) / (double) max_cost_contribution);
						fill(r, g, 0);
					} else
						fill(0, 255, 255);
					int x = i * machine_width;
					int y = j * resource_height;
					int width = machine_width;
					int height = resource_height;
					rect(x, y, width, height);
				}
			}
			if (mousePressed) {
				int machine = mouseX / machine_width;
				int resource = mouseY / resource_height;
				if ((machine < problem.M) && (machine >= 0)  && (resource < problem.RE) && (resource >=0)) {
					String msg = String
							.format("m%dr%d=%,d",
									machine,
									resource,
									currentSolution.load_cost_contribution_unzipped[machine][resource]);
					// System.err.println(msg);
					fill(0, 0, 0);
					text(msg, mouseX, mouseY);
				}
			}
		}
	}

	public void setCurrentSolution(Solution current_solution) {
		this.currentSolution = current_solution;

	}
}
